package com.zy.gpdemo.service;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.*;

public class Internet implements java.io.Serializable{
	private static final long serialVersionUID = 1L;
	private static InputStream is;
    private static OutputStream os;
    private Socket socket = null;
    public byte[] readBuffer = new byte[100];
    public Internet() {
    }

    public boolean openNetPort(String ip, int port) {
        try {
            this.socket = new Socket(ip, port);
            os = this.socket.getOutputStream();
            is = this.socket.getInputStream();
            return true;
        } catch (UnknownHostException var4) {
            var4.printStackTrace();
            System.out.println("ip地址错误");
            return false;
        } catch (ConnectException var5) {
            System.out.println("连接超时");
            return false;
        } catch (IOException var6) {
            var6.printStackTrace();
            System.out.println("获取流错误");
            return false;
        }
    }

    public boolean closeNetPort() {
        if (this.socket != null) {
            try {
                this.socket.close();
                os.close();
                is.close();
                return true;
            } catch (IOException var2) {
                var2.printStackTrace();
                return false;
            }
        } else {
            return false;
        }
    }

    public boolean sendMessage(String message) {
        try {
            if (os != null) {
                os.write(message.getBytes("gb2312"));
                return true;
            } else {
                System.out.println("请打开网口!");
                return false;
            }
        } catch (IOException var3) {
            return false;
        }
    }

    public boolean sendMessage(byte[] b) {
        try {
            if (os != null) {
                os.write(b);
                return true;
            } else {
                System.out.println("请打开网口!");
                return false;
            }
        } catch (IOException var3) {
            return false;
        }
    }

    public boolean readMessage() {
        try {
            if (is != null) {
                is.read(this.readBuffer);
                return true;
            } else {
                System.out.println("请打开网口");
                return false;
            }
        } catch (IOException var2) {
            return false;
        }
    }
}
